#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <unistd.h>
#include <sys/shm.h>

#define PATHNAME "tmp"
#define PROJ_ID 88
#define SIZE 4096

int main()
{
  key_t k =ftok(PATHNAME, PROJ_ID);
  printf("key值:%#X\n", k);

  int shmid = shmget(k, SIZE, IPC_CREAT | IPC_EXCL | 0666);
  if(shmid < 0)
  {
    perror("create failed");
    return 1;
  }

  char* shmaddr = (char*)shmat(shmid, NULL, 0);
  int count = 0;
  while(1)
  {
    count++;
    sleep(1);
    printf("%s\n", shmaddr);

  }

  shmdt(shmaddr);
  shmctl(shmid, IPC_RMID, NULL);

  return 0;
}
